/*****************************************************************************
 * Copyright (c) 2022, Nations Technologies Inc.
 *
 * All rights reserved.
 * ****************************************************************************
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Nations' name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ****************************************************************************/

/**
 * @file main.h
 * @author Nations
 * @version V1.2.2
 *
 * @copyright Copyright (c) 2022, Nations Technologies Inc. All rights reserved.
 */
#ifndef __MY_GPIO_H__
#define __MY_GPIO_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "n32l40x.h"
/************************************************************
gpio 功能定义，在本文件中定义
************************************************************/

// POWER_MODE_KEY, LEVEL_ADD_KEY 平时为高电平，按键动作时为低点平

#define POWER_MODE_KEY_PORT         GPIOD 
#define POWER_MODE_KEY_PIN          GPIO_PIN_14  

#define LEVEL_ADD_KEY_PORT          GPIOD
#define LEVEL_ADD_KEY_PIN           GPIO_PIN_15

// 马达+电磁铁+光疗灯 llt 前级power端控制 引脚
#define MOTOR_FE_LLT_POWER_PORT     GPIOC
#define MOTOR_FE_LLT_POWER_PIN      GPIO_PIN_15
#define MOTOR_FE_LLT_POWER_ON()     set_pin_high(MOTOR_FE_LLT_POWER_PORT, MOTOR_FE_LLT_POWER_PIN)
#define MOTOR_FE_LLT_POWER_OFF()    set_pin_low(MOTOR_FE_LLT_POWER_PORT, MOTOR_FE_LLT_POWER_PIN)

// 光疗灯 llt 后记输出端控制 引脚
#define LLT_CTRL_EN_PORT            GPIOC
#define LLT_CTRL_EN_PIN             GPIO_PIN_14
#define LLT_LED_ON()                set_pin_high(LLT_CTRL_EN_PORT, LLT_CTRL_EN_PIN)
#define LLT_LED_OFF()               set_pin_low(LLT_CTRL_EN_PORT, LLT_CTRL_EN_PIN)

// 马达 + 电磁铁（可以使用timer5_ch2, 比如马达更加档位，调节pwm占空比）
#define MOTOR_FE_EN_PORT            GPIOA
#define MOTOR_FE_EN_PIN             GPIO_PIN_1


//---------------BUZZER------
#define  BUZZ_USE_TIMER      1
#define  BUZZ_USE_GPIO       2
#define  BUZZ_MODE_SEL       (BUZZ_USE_GPIO)

#if(BUZZ_MODE_SEL == BUZZ_USE_TIMER)
#define  BUZZER_PORT        (GPIOB)   //timer4_ch4
#define  BUZZER_PIN         (GPIO_PIN_9)
#else
#define  BUZZER_PORT        (GPIOA)   //NORMAL GPIO
#define  BUZZER_PIN         (GPIO_PIN_11)

#endif

//*********高周波输出控制引脚*********** */
#define HF_OUTPUT_CTRL_PORT    GPIOA
#define HF_OUTPUT_CTRL_PIN     GPIO_PIN_5

#define HF_OUTPUT_ENABLE()     set_pin_high(HF_OUTPUT_CTRL_PORT, HF_OUTPUT_CTRL_PIN)
#define HF_OUTPUT_DISABLE()    set_pin_low(HF_OUTPUT_CTRL_PORT, HF_OUTPUT_CTRL_PIN)

/************EMS 模块引脚************************* */
// EMS  升压使能引脚
#define EMS_POWER_EN_PORT       GPIOB
#define EMS_POWER_EN_PIN        GPIO_PIN_14
#define EMS_POWER_ON()          set_pin_high(EMS_POWER_EN_PORT, EMS_POWER_EN_PIN)
#define EMS_POWER_OFF()         set_pin_low(EMS_POWER_EN_PORT, EMS_POWER_EN_PIN)

// ems 输出控制引脚
#define EMS_OUTPUT_CTRL_PORT    GPIOA
#define EMS_OUTPUT_CTRL_PIN     GPIO_PIN_12
#define EMS_OUTPUT_ENABLE()     set_pin_high(EMS_OUTPUT_CTRL_PORT, EMS_OUTPUT_CTRL_PIN)
#define EMS_OUTPUT_DISABLE()    set_pin_low(EMS_OUTPUT_CTRL_PORT, EMS_OUTPUT_CTRL_PIN)

/***************************************
 *  ems输出检测脚
 *  硬件电路说明---
 *      无负载时，ems检测脚位为高电平
 *      有负载时，ems脚位为低电平。
 ***********************************/ 
#define EMS_LOAD_DET_PORT        (GPIOA)
#define EMS_LOAD_DET_PIN         (GPIO_PIN_10)
#define IS_EMS_LOAD_ACTIVE()     (Bit_RESET == GPIO_ReadInputDataBit(EMS_LOAD_DET_PORT, EMS_LOAD_DET_PIN))

//**************EMS_PWM **************
#define  EMS_P_PWM_PORT          (GPIOA)   
#define  EMS_P_PWM_PIN           (GPIO_PIN_15)

#define  EMS_N_PWM_PORT          (GPIOB)   
#define  EMS_N_PWM_PIN           (GPIO_PIN_3)

//**************MODE_CHECK_VCC **************
#define MODE_CHECK_POWER_PORT       GPIOA
#define MODE_CHECK_POWER_PIN        GPIO_PIN_9
#define MODE_CHECK_POWER_ON()       set_pin_high(MODE_CHECK_POWER_PORT, MODE_CHECK_POWER_PIN)
#define MODE_CHECK_POWER_OFF()      set_pin_low(MODE_CHECK_POWER_PORT, MODE_CHECK_POWER_PIN)

//**************LED **************

#define WHITE_LED1_PORT       GPIOB
#define WHITE_LED1_PIN        GPIO_PIN_11
#define WHITE_LED1_ON()       set_pin_high(WHITE_LED1_PORT, WHITE_LED1_PIN)
#define WHITE_LED1_OFF()      set_pin_low(WHITE_LED1_PORT, WHITE_LED1_PIN)

#define RED_LED1_PORT         GPIOB
#define RED_LED1_PIN          GPIO_PIN_10
#define RED_LED1_ON()         set_pin_high(RED_LED1_PORT, RED_LED1_PIN)
#define RED_LED1_OFF()        set_pin_low(RED_LED1_PORT, RED_LED1_PIN)
//---第2组灯 ---
#define WHITE_LED2_PORT       GPIOB
#define WHITE_LED2_PIN        GPIO_PIN_2
#define WHITE_LED2_ON()       set_pin_high(WHITE_LED2_PORT, WHITE_LED2_PIN)
#define WHITE_LED2_OFF()      set_pin_low(WHITE_LED2_PORT, WHITE_LED2_PIN)

#define RED_LED2_PORT         GPIOB
#define RED_LED2_PIN          GPIO_PIN_1
#define RED_LED2_ON()         set_pin_high(RED_LED2_PORT, RED_LED2_PIN)
#define RED_LED2_OFF()        set_pin_low(RED_LED2_PORT, RED_LED2_PIN)
//---第3组灯------
#define WHITE_LED3_PORT       GPIOB
#define WHITE_LED3_PIN        GPIO_PIN_0
#define WHITE_LED3_ON()       set_pin_high(WHITE_LED3_PORT, WHITE_LED3_PIN)
#define WHITE_LED3_OFF()      set_pin_low(WHITE_LED3_PORT, WHITE_LED3_PIN)

#define RED_LED3_PORT         GPIOA
#define RED_LED3_PIN          GPIO_PIN_7
#define RED_LED3_ON()         set_pin_high(RED_LED3_PORT, RED_LED3_PIN)
#define RED_LED3_OFF()        set_pin_low(RED_LED3_PORT, RED_LED3_PIN)


#if 0
#define SPI_MASTER_GPIO           GPIOB     // JE3新硬件版本改为PB3
#define SPI_MASTER_PIN_MOSI       GPIO_PIN_3    // JE3新硬件版本改为PB3
#else
#define SPI_MASTER_GPIO           GPIOB         // Jx1 空闲脚
#define SPI_MASTER_PIN_MOSI       GPIO_PIN_7    // Jx1 空闲脚

#define SOFT_IIC_DATA_GPIO           GPIOB   // Jx1 空闲脚 --调试使用
#define SOFT_IIC_DATA_PIN           GPIO_PIN_8    // Jx1 空闲脚

#define SOFT_IIC_SCL_GPIO           GPIOB         // Jx1 空闲脚
#define SOFT_IIC_SCL_PIN            GPIO_PIN_9    // Jx1 空闲脚

#endif

#if 0   // JE3 灯珠
//---------------------2024 10 17 check
// 全光谱灯珠+红光 LED 660nm[电极板上的大灯珠18pcs]
#define  LLLT_FULL_SPECTRUM_PORT        (GPIOB)
#define  LLLT_FULL_SPECTRUM_PIN         (GPIO_PIN_11)
#define  FULL_SPECTRUM_LED_ON()         set_pin_high(LLLT_FULL_SPECTRUM_PORT, LLLT_FULL_SPECTRUM_PIN)
#define  FULL_SPECTRUM_LED_OFF()        set_pin_low(LLLT_FULL_SPECTRUM_PORT, LLLT_FULL_SPECTRUM_PIN)

//红绿复合灯珠--共14pcs， 分为2组控制[6pcs + 8pcs] = 14pcs
#define  LLLT_R6_DLED_PORT      (GPIOC)            //(GPIOA)  v1.1 版本呢修正
#define  LLLT_R6_DLED_PIN       (GPIO_PIN_15)      //(GPIO_PIN_11)
#define  DLED_R6_ON()           set_pin_high(LLLT_R6_DLED_PORT, LLLT_R6_DLED_PIN)
#define  DLED_R6_OFF()          set_pin_low(LLLT_R6_DLED_PORT, LLLT_R6_DLED_PIN)

#define  LLLT_G6_DLED_PORT      (GPIOC)         // (GPIOB)  v1.1 版本呢修正
#define  LLLT_G6_DLED_PIN       (GPIO_PIN_14)   // (GPIO_PIN_2)
#define  DLED_G6_ON()           set_pin_high(LLLT_G6_DLED_PORT, LLLT_G6_DLED_PIN)
#define  DLED_G6_OFF()          set_pin_low(LLLT_G6_DLED_PORT, LLLT_G6_DLED_PIN)

//红绿复合灯珠--共14pcs， 分为2组控制[6pcs + 8pcs] = 14pcs
#define  LLLT_R8_DLED_PORT      (GPIOA)         //(GPIOC)  v1.1 版本呢修正
#define  LLLT_R8_DLED_PIN       (GPIO_PIN_11)    //   (GPIO_PIN_14)
#define  DLED_R8_ON()           set_pin_high(LLLT_R8_DLED_PORT, LLLT_R8_DLED_PIN)
#define  DLED_R8_OFF()          set_pin_low(LLLT_R8_DLED_PORT, LLLT_R8_DLED_PIN)

#define  LLLT_G8_DLED_PORT    (GPIOB)       // (GPIOC)
#define  LLLT_G8_DLED_PIN     (GPIO_PIN_2) // (GPIO_PIN_15)
#define  DLED_G8_ON()         set_pin_high(LLLT_G8_DLED_PORT, LLLT_G8_DLED_PIN)
#define  DLED_G8_OFF()        set_pin_low(LLLT_G8_DLED_PORT, LLLT_G8_DLED_PIN)
#endif

// 斩脂刀
#define ZHANZHIDAO_IN_PORT              (GPIOC)
#define ZHANZHIDAO_IN_PIN               (GPIO_PIN_13)

#define IS_ZHANZHIDAO_ONLINE()         (1 == GPIO_ReadInputDataBit(ZHANZHIDAO_IN_PORT, ZHANZHIDAO_IN_PIN))

/************************************************************





************************************************************/

void gpio_output_cfg(GPIO_Module* GPIOx, uint16_t Pin);
void gpio_input_cfg(GPIO_Module* GPIOx, uint16_t Pin, GPIO_PuPdType pull_up_down_mode);
void set_pin_high(GPIO_Module* GPIOx, uint16_t Pin);
void set_pin_low(GPIO_Module* GPIOx, uint16_t Pin);
void set_pin_tirrgle(GPIO_Module* GPIOx, uint16_t Pin);

void fast_gpio_input_high_cfg(GPIO_Module* GPIOx, uint16_t Pin);
void fast_gpio_output_cfg(GPIO_Module* GPIOx, uint16_t Pin);


#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H__ */
